home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / examples / oclip.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  6KB  |  204 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. /*----------------------------------------------------------------------------
  41.  *
  42.  * oclip.c : openGL (motif) example showing how to use arbitrary clipping plane.
  43.  *
  44.  * Author : Yusuf Attarwala
  45.  *          SGI - Applications
  46.  * Date   : Mar 93
  47.  *
  48.  *    note : the main intent of this program is to demo the arbitrary
  49.  *           clipping functionality, hence the rendering is kept
  50.  *           simple (wireframe) and only one clipping plane is used.
  51.  *
  52.  *    press  left   button to move object 
  53.  *           right  button to move clipping plane
  54.  *
  55.  *
  56.  *---------------------------------------------------------------------------*/
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59.  
  60. #include <GL/glut.h>
  61.  
  62. /* function declarations */
  63.  
  64. void 
  65.     drawScene(void),
  66.     setMatrix(void),
  67.     animateClipPlane(void),
  68.     animation(void),
  69.     resize(int w, int h),
  70.     keyboard(unsigned char c, int x, int y);
  71.  
  72.  
  73. /* global variables */
  74.             
  75. float         ax,ay,az;             /* angles for animation */
  76. GLUquadricObj *quadObj;             /* used in drawscene */
  77. GLdouble planeEqn[] = {0.707,0.707,0.0,0.0};   /* initial clipping plane eqn */
  78.  
  79. int count = 0;
  80. int clip_count = 0;
  81.  
  82. void
  83. menu(int choice)
  84. {
  85.         switch (choice) {
  86.         case 1 :
  87.             count = 90;
  88.             glutIdleFunc(animation);
  89.             break;
  90.         case 2 :
  91.             animateClipPlane();
  92.             break;
  93.         }
  94. }
  95.  
  96. int 
  97. main(int argc, char** argv)
  98. {
  99.     glutInit(&argc, argv);
  100.  
  101.     quadObj = gluNewQuadric ();   /* this will be used in drawScene */
  102.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  103.     glutCreateWindow("Arbitrary clip plane");
  104.  
  105.     ax = 10.0;
  106.     ay = -10.0;
  107.     az = 0.0;
  108.  
  109.     glutDisplayFunc(drawScene);
  110.     glutReshapeFunc(resize);
  111.     glutKeyboardFunc(keyboard);
  112.     glutCreateMenu(menu);
  113.     glutAddMenuEntry("Rotate", 1);
  114.     glutAddMenuEntry("Move clip plane", 2);
  115.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  116.     glutMainLoop();
  117.     return 0;             /* ANSI C requires main to return int. */
  118. }
  119.  
  120. void
  121. drawScene(void)
  122. {
  123.     glClearColor(0.0, 0.0, 0.0, 0.0);
  124.     glClear(GL_COLOR_BUFFER_BIT);
  125.  
  126.     glPushMatrix();
  127.     gluQuadricDrawStyle (quadObj, GLU_LINE);
  128.     glColor3f (1.0, 1.0, 0.0);
  129.     glRotatef (ax,1.0,0.0,0.0);
  130.     glRotatef (-ay,0.0, 1.0, 0.0);
  131.  
  132.     glClipPlane(GL_CLIP_PLANE0,planeEqn);      /* define clipping plane */
  133.     glEnable(GL_CLIP_PLANE0);                  /* and enable it */
  134.  
  135.     gluCylinder (quadObj, 2.0,5.0,10.0,20,8);  /* draw a cone */
  136.  
  137.     glDisable(GL_CLIP_PLANE0);
  138.     glPopMatrix();
  139.  
  140.     glutSwapBuffers();
  141. }
  142.  
  143. void
  144. setMatrix(void)
  145. {
  146.     glMatrixMode(GL_PROJECTION);
  147.     glLoadIdentity();
  148.     glOrtho(-15.0,15.0,-15.0,15.0,-10.0,10.0);
  149.     glMatrixMode(GL_MODELVIEW);
  150.     glLoadIdentity();
  151. }
  152.  
  153. void
  154. animation(void)
  155. {
  156.    if(count) {
  157.         ax += 5.0;
  158.         ay -= 2.0;
  159.         az += 5.0;
  160.         if (ax >= 360)  ax = 0.0;
  161.         if (ay <= -360) ay = 0.0;
  162.         if (az >= 360)  az = 0.0;
  163.         glutPostRedisplay();
  164.         count--;
  165.     }
  166.     if(clip_count) {
  167.     static int sign = 1;
  168.  
  169.         planeEqn[3] += sign*0.5;
  170.         if (planeEqn[3] > 4.0) sign = -1;
  171.         else if (planeEqn[3] < -4.0) sign = 1;
  172.         glutPostRedisplay();
  173.         clip_count--;
  174.     }
  175.     if(count <= 0 && clip_count <= 0) glutIdleFunc(NULL);
  176. }
  177.  
  178. void
  179. animateClipPlane(void)
  180. {
  181.     clip_count = 5;
  182.     glutIdleFunc(animation);
  183. }
  184.  
  185. void
  186. keyboard(unsigned char c, int x, int y)
  187. {
  188.     switch(c) {
  189.         case 27 :
  190.             exit(0);
  191.             break;
  192.     default:
  193.         break;
  194.     }
  195. }
  196.  
  197. void 
  198. resize(int w, int h)
  199. {
  200.     glViewport(0, 0, w, h);
  201.     setMatrix();
  202. }
  203.  
  204.